<template>
  <div class="content">
    <div class="top">
      <div class="task_name">任务名称：{{ task_name }}</div>
      <!-- <div class="btn-wrap" v-if="tree_selected_arr.length > 0"> -->
      <div class="btn-wrap">
        <el-button type="primary" size="small" @click="submit()"
          >保存 <i class="el-icon-upload2"></i
        ></el-button>
        <el-button type="success" size="small" @click="play()"
          >播放 <i class="el-icon-video-play"></i
        ></el-button>
      </div>
    </div>
    <!-- <div class="wrap"> -->
    <draggable
      v-model="tree_selected_arr"
      chosenClass="chosen"
      forceFallback="true"
      group="people"
      animation="1000"
    >
      <transition-group class="item-con">
        <div
          class="item"
          v-for="(item, index) in tree_selected_arr"
          :key="index"
        >
          <div
            class="el-icon-circle-close close"
            @click="fnClose(index, item)"
          ></div>
          <div class="parent_label">
            {{ item.device_name ? item.device_name : item.alias }}
          </div>
          <div class="chns_label" v-if="!!item.channel">
            通道{{ Number(item.channel) + 1 }}
          </div>
        </div>
      </transition-group>
    </draggable>
    <!-- </div> -->
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import draggable from "vuedraggable";

export default {
  name: "wrapTask",
  props: {},
  data() {
    return {
      task_id: "",
      task_name: "",
      loop_interval: "",
      res_arr: [],
      task_arr: [],
    };
  },
  computed: {
    tree_selected_arr() {
      return this.$store.state.playTask.tree_selected_arr || [];
    },
  },
  created() {},
  mounted() {
    this.task_id = this.$route.query.task_id;
    this.task_name = this.$route.query.task_name;
    this.loop_interval = this.$route.query.loop_interval;
    this.init();
  },
  methods: {
    submit() {
      let devices = this.tree_selected_arr;
      let that = this;
      let arr = [];
      if (Array.isArray(devices) && devices.length > 0) {
        for (let i = 0; i < devices.length; i++) {
          devices[i]["play_order"] = i + 1;
        }
      }
      postData("/adminweb/video/live/playing/looptask/device/set", {
        task_id: that.task_id,
        devices,
      })
        .then((res) => {
          if (res.code == 0) {
            that.$message.success("提交成功！");
            that.$emit("refreshTableList");

            setTimeout(function() {
             that.$router.go(-1)
            }, 2000);
          } else {
            that.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          that.$message.error(res.code + "，" + res.msg);
        });
    },
    init() {
      this.task_arr = [];
      postData("/adminweb/video/live/playing/looptask/device/list", {
        task_id: this.task_id,
      })
        .then((res) => {
          let res_devices = res.datas.devices;
          this.$store.commit("playTask/initTaskArr", res_devices);
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },

    fnClose(index, item) {
      this.$store.commit("playTask/removeTaskArr", { index, item });
    },

    play() {
      let task_id = this.task_id;
      let task_name = this.task_name;
      let loop_interval = this.loop_interval;

      this.$router.push({
        path: "/playTask/playAuto",
        query: {
          task_id: task_id,
          task_name: task_name,
          loop_interval: loop_interval,
        },
      });
    },
  },
  components: {
    draggable,
  },
};
</script>
<style scoped>
.content {
  position: relative;
  width: 100%;
  height: calc(100% - 60px);
  box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;
}
.top {
  height: 60px;
  padding-left: 20px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.task_name {
  color: #fff;
}
.btn-wrap {
  margin-left: 50px;
}
/* .wrap {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
} */
.item-con {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
}
.item {
  width: 17.5%;
  height: 100px;
  border: 1px solid #5b6372;
  border-radius: 8px;
  margin: 15px;
  color: #fff;
  font-size: 14px;
  padding: 12px;
  position: relative;
  cursor: move;
}

.chns_label {
  color: #6cbddd;
}

.close {
  color: #fff;
  cursor: pointer;
  position: absolute;
  right: -10px;
  top: -10px;
  font-size: 20px;
}
</style>
